﻿using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 卡池(单例类)
/// 3,4,5,6,7,8,9,10,J,Q,K,A,2
/// 方块
/// 1,2,3,4,5,6,7,8,9,10,11,12,13
/// 梅花
/// 14,15,16,17,18,19,20,21,22,23,24,25,26
/// 黑桃
/// 27,28,29,30,31,32,33,34,35,36,37,38,39
/// 红桃
/// 40,41,42,43,44,45,46,47,48,49,50,51,52
/// </summary>
public class BattleCardPoolLogic
{
    /// <summary>
    /// 单例对象
    /// </summary>
    private static BattleCardPoolLogic _cardPool;

    /// <summary>
    /// 总卡池字典
    /// </summary>
    private List<ObstacleDataModel> gameCardPool;

    /// <summary>
    /// 牌大小数组
    /// </summary>
    private string[] card = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };

    /// <summary>
    /// 花色数组
    /// </summary>
    /// <returns></returns>
    private string[] Decor = { "fk", "mh", "ht", "hongt" };

    /// <summary>
    /// getInstance
    /// </summary>
    /// <returns></returns>
    public static BattleCardPoolLogic getInstance()
    {
        if (_cardPool == null)
        {
            _cardPool = new BattleCardPoolLogic();
        }

        return _cardPool;
    }
    
    /// <summary>
    /// 初始化池子
    /// </summary>
    public void InitPool()
    {
        if (gameCardPool == null)
        {
            gameCardPool = new List<ObstacleDataModel>();
        }
        else
        {
            gameCardPool.Clear();
        }
        
        //52张牌
        for (int index = 1; index <=52; index++)
        {
            ObstacleDataModel cardDataModel_ = new ObstacleDataModel();
            cardDataModel_.SetCardNum(index);
            cardDataModel_.setCardSpriteName(GetCardInfoByCardNum(index));
            cardDataModel_.SetKind(4);
            gameCardPool.Add(cardDataModel_);
        }
    }

    /// <summary>
    /// 添加某张牌
    /// </summary>
    /// <param name="cardNum"></param>
    public void addCardInPool(int cardNum)
    {
        foreach (ObstacleDataModel card in gameCardPool)
        {
            if (card.GetCardNum() == cardNum)
            {
                // gameCardPool.Remove(card);
                Debug.LogError(GetCardInfoByCardNum(cardNum) + "已经在战场牌库中");
                return;
            }
        }

        ObstacleDataModel cardDataModel_ = new ObstacleDataModel();
        cardDataModel_.SetCardNum(cardNum);
        cardDataModel_.setCardSpriteName(GetCardInfoByCardNum(cardNum));
        cardDataModel_.SetKind(4);
        // Debug.Log("战场牌库添加"+GetCardInfoByCardNum(cardNum));
        gameCardPool.Add(cardDataModel_);
    }

    /// <summary>
    /// 移除战场池中的扑克
    /// </summary>
    public void RemoveCardInBattlePoolBySpriteName(string spriteName)
    {
        // Debug.Log("spriteName----------------------"+spriteName);
        for(int index = 0;index<gameCardPool.Count;index++)
        {
            if (gameCardPool[index].GetCardSpriteName()==spriteName)
            {
                gameCardPool.Remove(gameCardPool[index]);
            }
        }
    }


    /// <summary>
    /// 随机从池子里取牌
    /// </summary>
    public ObstacleDataModel randomGetCardInPool()
    {
        if (gameCardPool.Count > 0)
        {
            int random_key = UnityEngine.Random.Range(0, gameCardPool.Count);
            ObstacleDataModel cardDataModel_ = gameCardPool[random_key];
            if (cardDataModel_ != null)
            {
                // Debug.Log("战场牌库删除"+cardDataModel_.GetCardSpriteName());
                gameCardPool.RemoveAt(random_key);
                return cardDataModel_;
            }
            else
            {
                Debug.LogError("随机的" + random_key + "获取到为空");
                return null;
            }
        }
        else
        {
            Debug.LogError("池子空了");
            return null;
        }
    }
    /// <summary>
    /// 获取战场牌池的牌数量
    /// </summary>
    /// <returns></returns>
    public int GetCardCountInBattleCardPool()
    {
        return gameCardPool.Count;
    }


    /// <summary>
    /// 通过名字添加卡
    /// </summary>
    public void AddCardInBattlePoolBySpriteName(string spriteName)
    {
        //将玩家丢弃的扑克加入到战场池中
        addCardInPool(spriteNameToCardNum(spriteName));
    }

    /// <summary>
    /// 图片名字转数字
    /// </summary>
    /// <returns></returns>
    public int spriteNameToCardNum(string spriteName)
    {
        string[] spriteNameArray = spriteName.Split('_');
        int target_0 = 0;
        for (int index = 0; index < Decor.Length; index++)
        {
            if (Decor[index] == spriteNameArray[0])
            {
                target_0 = index;
                break;
            }
        }

        int target_1 = 0;
        for (int index = 0; index < card.Length; index++)
        {
            if (card[index] == spriteNameArray[1])
            {
                target_1 = index;
                break;
            }
        }
        
        int cardNum = target_0 * 13 + target_1;
        cardNum++;
        return cardNum;
    }
    
    /// <summary>
    /// 通过数组获得这张牌的花色和大小
    /// </summary>
    public String GetCardInfoByCardNum(int cardNum)
    {
        cardNum--;
        //检查是否合法
        if (cardNum >= 0 && cardNum <= 52)
        {
            string showText = "";
            //求花色
            int DecorIndex = cardNum / 13;
            showText = showText + Decor[DecorIndex];
            //求牌值
            int cardIndex = cardNum % 13;
            showText = showText + "_" + card[cardIndex];
            return showText;
        }
        else
        {
            Debug.LogError("cardNum越界不在0-52内" + cardNum);
            return null;
        }
    }
    
}